package io.bdmc.modules.bss.controller;

import java.io.File;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import io.bdmc.common.utils.Constant;
import io.bdmc.common.utils.R;
import io.bdmc.modules.sys.controller.AbstractController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HuangLu
 * @since 2019-09-11
 */
// "资源文件管理", description = "/bss/res")
@RestController
@RequestMapping("/bss/res")
public class ResourceController extends AbstractController {

	// "上传图片", notes = "上传图片")
	@RequestMapping("/image/upload")
	public R imageUpload(@RequestParam("file") MultipartFile file) throws Exception {
		File targetFile = null;
		String fileName = file.getOriginalFilename();// 获取文件名加后缀
		if (fileName != null && fileName != "") {
			fileName = System.currentTimeMillis()+"" ;
			File path = new File(Constant.EXPORTPATH);
			if (!path.exists()) {
				path.mkdir();
			}
			// 生成jpg图片
			String savePath = path.getAbsolutePath() + "/image/";// 新生成的图片
			File path1 = new File(savePath);
			if (!path1.exists()) {
				path1.mkdir();
			}
			targetFile = new File(path1, fileName);
			String serverPath = "/image/" + fileName;
			try {
				// 将上传的文件写到服务器上指定的文件。
				file.transferTo(targetFile);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return R.ok().putData(serverPath);
		} else {
			return R.error(-1, "请上传图片文件");
		}
	}

	// "上传声音", notes = "上传声音")
	@RequestMapping("/voice/upload")
	public R voiceUpload(@RequestParam("file") MultipartFile file) throws Exception {
		File targetFile = null;
		String fileName = file.getOriginalFilename();// 获取文件名加后缀
		if (fileName != null && fileName != "" && (fileName.endsWith(".wav") || fileName.endsWith(".mp3"))) {
			fileName = System.currentTimeMillis() + fileName;
			File path = new File(Constant.EXPORTPATH);
			if (!path.exists()) {
				path.mkdir();
			}
			String savePath = path.getAbsolutePath() + "/voice/";// 新生成的声音文件
			File path1 = new File(savePath);
			if (!path1.exists()) {
				path1.mkdir();
			}
			targetFile = new File(path1, fileName);
			String serverPath = "/voice/" + fileName;
			try {
				// 将上传的文件写到服务器上指定的文件。
				file.transferTo(targetFile);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return R.ok().putData(serverPath);
		} else {
			return R.error(-1, "请上传声音文件");
		}
	}

	// "上传固件", notes = "上传固件")
	@RequestMapping("/bios/upload")
	public R mibUpload(@RequestParam("file") MultipartFile file) throws Exception {
		File targetFile = null;
		String fileName = file.getOriginalFilename();// 获取文件名加后缀
		if (fileName != null && fileName != "") {
			File path = new File(Constant.EXPORTPATH);
			if (!path.exists()) {
				path.mkdir();
			}
			String savePath = path.getAbsolutePath() + "/bios/";// 新生成的声音文件
			File path1 = new File(savePath);
			if (!path1.exists()) {
				path1.mkdir();
			}
			targetFile = new File(path1, fileName);
			String serverPath = "/bios/" + fileName;
			try {
				// 将上传的文件写到服务器上指定的文件。
				file.transferTo(targetFile);
				// FTPUtils.uploadFile(serverPath, "/nand/module/firmware/", "ftpIp",
				// ftpUsername, ftpPwd)
				// FTPUtils.downFile(remotePath, fileName, ftpIp, ftpUsername, ftpPwd)
			} catch (Exception e) {
				e.printStackTrace();
			}
			return R.ok().putData(serverPath);
		} else {
			return R.error(-1, "请上传声音文件");
		}
	}
}
